package com.example.mysaat.service_menu_manual_test.allActivities;


import android.animation.TimeAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.storage.DiskInfo;
import android.os.storage.StorageEventListener;
import android.os.storage.StorageManager;
import android.os.storage.VolumeInfo;
import android.os.storage.VolumeRecord;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mysaat.R;
import com.example.mysaat.service_menu_manual_test.Listeners.MyFailOnclickListener;
import com.example.mysaat.service_menu_manual_test.Listeners.MyPassOnclickListener;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class OTGActivity extends Activity {
    private Button fail_btn,pass_btn;
    private TextView fail_text,pass_text;
    private StorageManager mStorageManager;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otg);
        init();
        initContent();
        mStorageManager = getSystemService(StorageManager.class);
    }


    @Override
    protected void onResume() {
        super.onResume();
        Method method = null;
        try {
            method = StorageManager.class.getDeclaredMethod("registerListener", StorageEventListener.class);
            method.setAccessible(true);
            method.invoke(mStorageManager,storageEventListener);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        Method method = null;
        try {
            method = StorageManager.class.getDeclaredMethod("unregisterListener", StorageEventListener.class);
            method.setAccessible(true);
            method.invoke(mStorageManager,storageEventListener);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private void init(){
        fail_btn = findViewById(R.id.fail_button);
        pass_btn = findViewById(R.id.pass_button);
        fail_text = findViewById(R.id.otg_text_result);
        pass_text = findViewById(R.id.otg_operation_result);
    }

    private void initContent(){
        fail_btn.setOnClickListener(new MyFailOnclickListener(OTGActivity.this,null));
        pass_btn.setOnClickListener(new MyPassOnclickListener(OTGActivity.this,null));
    }

    private StorageEventListener storageEventListener = new StorageEventListener(){

        @Override
        public void onUsbMassStorageConnectionChanged(boolean connected) {
            super.onUsbMassStorageConnectionChanged(connected);
        }

        @Override
        public void onStorageStateChanged(String path, String oldState, String newState) {
            super.onStorageStateChanged(path, oldState, newState);
        }

        @Override
        public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
            super.onVolumeStateChanged(vol, oldState, newState);
            if(vol.getType()==VolumeInfo.TYPE_PUBLIC&&oldState!=newState&&newState==VolumeInfo.STATE_MOUNTED){
                Toast.makeText(OTGActivity.this,"remounted success", Toast.LENGTH_SHORT).show();
                fail_btn.setEnabled(true);
                fail_text.setText("pass");
                fail_text.setBackgroundColor(Color.YELLOW);
                pass_btn.setVisibility(View.VISIBLE);
                pass_btn.setEnabled(false);
            }
        }

        @Override
        public void onVolumeRecordChanged(VolumeRecord rec) {
            super.onVolumeRecordChanged(rec);
        }

        @Override
        public void onVolumeForgotten(String fsUuid) {
            super.onVolumeForgotten(fsUuid);
        }

        @Override
        public void onDiskScanned(DiskInfo disk, int volumeCount) {
            super.onDiskScanned(disk, volumeCount);
        }

        @Override
        public void onDiskDestroyed(DiskInfo disk) {
            super.onDiskDestroyed(disk);
        }
    };

    }